package overlays;

import java.util.ArrayList;
import XMLread.Space;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * This class is used to draw an overlay of the classrooms on a building floor
 * onto a given google mapview, based on ArrayLists of GeoPoints.
 * 
 * @author Nick
 * @version 1
 **/
public class FloorOverlay extends Overlay {
	private ArrayList<Space> spaces;	
	
	/**
	 * A constructor for this overlay.
	 * @param route the ArrayList of GeoPoints for the floor to be overlaid.
	 */
	public FloorOverlay(ArrayList<Space> spaces) {
		super();		
		this.spaces = spaces;
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		for(Space space : spaces) {
			drawPath(mv, canvas, space);
		}
		return true;
	}

	/**
	 * This method draws a class on a given MapView.
	 * @param mv the MapView that the floor will be drawn onto.
	 * @param canvas the canvas which contains the MapView.
	 */
	public void drawPath(MapView mv, Canvas canvas, Space space) {
		int xPrev = -1;
		int yPrev = -1; 
		int xNow = -1; 
		int yNow = -1;			
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeWidth(3);
		paint.setAlpha(100);
		if (space.getArrayList() != null) {
			for (int i = 0; i < space.getArrayList().size(); i++) {
				GeoPoint gp = space.getArrayList().get(i);
				Point point = new Point();
				mv.getProjection().toPixels(gp, point);
				xNow = point.x;
				yNow = point.y;
				if (xPrev != -1)
					canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);
				xPrev = xNow;
				yPrev = yNow;
			}
		}
	}	
}